package com.example.mycontact.DB

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.util.Log

class MyDBHelper(var context: Context):
    SQLiteOpenHelper(context,"ContactDB",null,2){
    override fun onCreate(db: SQLiteDatabase?) {
        //数据库创建时执行 只 ejecarse一次，创建一些初始表
        var createTable="create table Contacts("+"id integer primary key autoincrement,"+"name text not null,"+ "company text,"+"position text,"+"tel text," + "mail text,"+"image BLOB,"+"isFavorite integer default 0)"
        //执行创建表的sql
        db?.execSQL(createTable)
        Log.i("MyDBHelper","contacts数据表创建成功")
    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        // 升级数据库时的处理
        if (oldVersion < 2) {
            // 添加 isFavorite 字段
            db?.execSQL("ALTER TABLE Contacts ADD COLUMN isFavorite integer default 0")
            Log.i("MyDBHelper", "Contacts表升级：添加isFavorite字段")
        }
    }
}